package com.yanqu.road.dao.impl.monopolymarket.office;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.monopolymarket.office.MonopolyMarketOfficeSeatData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author cwq
 * @Data 2021/8/24 18:34
 */
public class MonopolyMarketOfficeSeatDataDaoImpl extends TempDataObjectDao<MonopolyMarketOfficeSeatData> {

    public static String getMonopolyMarketOfficeSeatDataSql = "select * from `t_c_monopolymarket_office_seat` where `groupId`=? and `posIndex`>=?  order by `posIndex`";
    public List<MonopolyMarketOfficeSeatData> getMonopolyMarketOfficeSeatData(long groupId,int startIndex) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<MonopolyMarketOfficeSeatData> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getMonopolyMarketOfficeSeatDataSql);
            int i = 1;
            call.setLong(i++, groupId);
            call.setInt(i++, startIndex);
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String updateMonopolyMarketOfficeSeatDataSql = "update `t_c_monopolymarket_office_seat` set userId=?,seatTime=?,decorationClass=?,decorationOverTime=?,changeTime=? where groupId=? and posIndex=?";
    @Override
    protected boolean updateData(MonopolyMarketOfficeSeatData monopolyMarketOfficeSeatData) {
        monopolyMarketOfficeSeatData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketOfficeSeatData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketOfficeSeatData.getSeatTime()));
        para.put(i++, new DbParameter(Types.TINYINT, monopolyMarketOfficeSeatData.getDecorationClass()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketOfficeSeatData.getDecorationOverTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketOfficeSeatData.getChangeTime()));
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketOfficeSeatData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketOfficeSeatData.getPosIndex()));
        if (getDb().executeNoneQuery(updateMonopolyMarketOfficeSeatDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertMonopolyMarketOfficeSeatDataSql = "insert into `t_c_monopolymarket_office_seat`(groupId,posIndex,userId,seatTime,decorationClass,decorationOverTime,changeTime) values (?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(MonopolyMarketOfficeSeatData monopolyMarketOfficeSeatData) {
        monopolyMarketOfficeSeatData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketOfficeSeatData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketOfficeSeatData.getPosIndex()));
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketOfficeSeatData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketOfficeSeatData.getSeatTime()));
        para.put(i++, new DbParameter(Types.TINYINT, monopolyMarketOfficeSeatData.getDecorationClass()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketOfficeSeatData.getDecorationOverTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketOfficeSeatData.getChangeTime()));
        if (getDb().executeNoneQuery(insertMonopolyMarketOfficeSeatDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected MonopolyMarketOfficeSeatData parseData(ResultSet rs) throws SQLException {
        MonopolyMarketOfficeSeatData monopolyMarketOfficeSeatData = new MonopolyMarketOfficeSeatData();
        monopolyMarketOfficeSeatData.setGroupId(rs.getLong("groupId"));
        monopolyMarketOfficeSeatData.setPosIndex(rs.getShort("posIndex"));
        monopolyMarketOfficeSeatData.setUserId(rs.getLong("userId"));
        monopolyMarketOfficeSeatData.setSeatTime(rs.getInt("seatTime"));
        monopolyMarketOfficeSeatData.setDecorationClass(rs.getByte("decorationClass"));
        monopolyMarketOfficeSeatData.setDecorationOverTime(rs.getInt("decorationOverTime"));
        monopolyMarketOfficeSeatData.setChangeTime(rs.getInt("changeTime"));

        return monopolyMarketOfficeSeatData;
    }
}
